//页面加载完成
$(function () {


    var  cases_datagrid =  $("#cases_datagrid");

    var cases_form =  $("#cases_form");

    var cases_dialog =  $("#cases_dialog");

    var combobox_role =  $("#combobox_role");
    var searchBox=$("#searchBox");

    searchBox.searchbox({
        searcher:function(value,name){
            cases_datagrid.datagrid("load",{keyword:value});
        },
        prompt:'关键字'
    });

    //初始员工列表界面
   cases_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: '/cases/list',
        rownumbers: true,
        pagination: true,
        singleSelect: true,
        columns: [[
            {field: 'id', hidden: true},
            {field: 'name', title: '报案人姓名', width: 100},
            {field: 'receiveTime', title: '发生时间', width: 100},
            {field: 'address', title: '事故地点', width: 100},
            {field: 'employee', title: '接通员工', width: 100, formatter: employeeFormatter},
            {field: 'sex', title: '报案人性别', width: 100},
            {field: 'carNumber', title: '车牌号', width: 100},
            {field: 'state', title: '状态', width: 100,formatter: statusFormatter},
            {field: 'tel', title: '联系电话', width: 100},
            {field: 'policySn', title: '保单编号', width: 100,formatter: policySnFormatter}

        ]],
        toolbar: "#tb",
       onLoadSuccess:function(data){
           $("a[name='opera']").linkbutton({plain:true,iconCls:'icon-search'});
       }

    })
        //将方法绑定到对象中
        var cmdObj = {

            addCases:function(){

                //清空form表单中的数据
                cases_form.form("clear");
                //设置title表单名
                cases_dialog.dialog("setTitle", "新增");
                //弹出对话框
                cases_dialog.dialog("open");
            },
            editCases:function () {
                //选中一条需要删除的数据
                var row = cases_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert("温馨提示", "亲,请选择一条需要编辑的数据", "error");
                    return;
                }
                if (row.state) {
                    $.messager.alert("温馨提示", "亲,此数据已处理", "error");
                    return;
                }
                //弹出对话框
                //设置title表单名
                cases_dialog.dialog("setTitle", "编辑");
                //弹出对话框
                cases_dialog.dialog("open");
                //清空form表单中的数据
                cases_form.form("clear");
                cases_form.form("load", row);
            },

            timeSearch:function () {
                var beginDate=$("#beginDate").val();
                var endDate=$("#endDate").val();
                var value=$("#searchBox").val();
                cases_datagrid.datagrid("load",{keyword:value,beginDate:beginDate,endDate:endDate});
            },

            reloadCases:function () {
                cases_datagrid.datagrid("reload");
            },
            saveCases:function () {
                var id = $("input[name='id']").val();
                var url;
                if (!id) {
                    url = "/cases/save";
                } else {

                    url = "/cases/update"
                }

                //发送一条资源去访问后台
                cases_form.form('submit', {
                    url: url,
                    success: function (data) {
                        data = $.parseJSON(data);
                        if (data.success) {
                            $.messager.alert("温馨提示","保存成功", "info", function () {
                               cmdObj.cancelCases();
                                cmdObj.reloadCases();
                            });
                        } else {
                            $.messager.alert("温馨提示", data.message, "error")
                        }
                    }
                });
            },
            cancelCases:function () {
                cases_dialog.dialog("close");
            }
        }
        //使用js来绑定事件  使用了属性过滤器来完成该操作
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        //调用方法
        cmdObj[cmd]();
    });


    //end
    //初始化对话框
    $("#cases_dialog").dialog({
        width: 350,
        height: 470,
        closed: true,
        buttons: "#bb"
    })


})



//部门格式化操作
function employeeFormatter(value, row, index) {
    return value?value.userName:"";
}
//接通员工格式化
function statusFormatter(value, row, index) {
    if (value==0){
        return '<span style="color: red">待处理</span>';
    }else if (value==1){
        return "已处理";
    }
}
//保单格式化
function policySnFormatter(value, row, index) {
    return   '<a name="opera" class="easyui-linkbutton" onclick="searchState('+row.policySn+')"></a>'+value;
}
//单独定义每行的点击事件
function searchState(policySn) {
    $.post("cases/searchState",{policySn:policySn},function (data) {
        if (data.success){
            $.messager.alert('查询结果',data.msg,'info');
        }else{
            $.messager.alert('查询结果',data.msg,'error');
        }
    })
}